﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace countlines
{
    class Program
    {
        static ConsoleFontColor sColor = new ConsoleFontColor();
        private static string sExePath = Application.StartupPath;

        private static string[] GetWantedFileType(string filename)
        {
            var objReader = new StreamReader(filename);
            String sLine = "";
            sLine = objReader.ReadLine();
            string[] type = sLine.Split(' ');
            objReader.Close();

            return type;
        }

        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
//                sColor.TextColor((int)ConsoleFontColor.Foreground.Green + (int)ConsoleFontColor.Foreground.Intensity);
                Console.WriteLine("  Version: v0.4");
                Console.WriteLine("  -h for help");
                return;
//                sColor.ResetColor();
            }

            if (args[0][0] == '-')
            {
                if (args[0].Length < 2)
                {
                    Console.WriteLine("  No such command, sorry.");
                    return;
                }
                switch (args[0][1])
                {
                    case 'h':
                        Console.WriteLine("  Directory:   start counting on Directory.");
                        Console.WriteLine("  -s:          set the wanted file type(s).");
                        Console.WriteLine("  -g:          get current set file type(s).");
                        Console.WriteLine("  -h:          ask for help.");
                        break;
                    case 's':
                        Console.WriteLine("  Input the wanted types in one line, use whitespace to separate.");
                        Console.WriteLine("  Do not input anything if you want to keep the old ones.");
                        sColor.TextColor((int)ConsoleFontColor.Foreground.Red + (int)ConsoleFontColor.Foreground.Intensity);
                        Console.WriteLine("  WARNING: do not forget the '.' !");
                        sColor.ResetColor();
                        Console.WriteLine("    e.g:");
                        Console.WriteLine("    .cpp .h .c .java .cs .py");
                        Console.WriteLine("    (default), (cpp) now supported");
                        Console.Write("  Input: ");
                        string input = Console.ReadLine();
                        if (input == "")
                        {
                            sColor.TextColor((int)ConsoleFontColor.Foreground.Red + (int)ConsoleFontColor.Foreground.Intensity);
                            Console.WriteLine("    Cancelled.");
                            sColor.ResetColor();
                            return;
                        }

                        if (input[0] == '(' && input[input.Length - 1] == ')')
                        {
                            switch (input)
                            {
                                case "(default)":
                                    input = ".cpp .h .c .java .cs .py";
                                    break;
                                case "(cpp)":
                                    input = ".cpp .c .hpp .h";
                                    break;
                                default:
                                    sColor.TextColor((int)ConsoleFontColor.Foreground.Red + (int)ConsoleFontColor.Foreground.Intensity);
                                    Console.WriteLine("    Unsupported!");
                                    sColor.ResetColor();
                                    return;
                                    break;
                            }
                        }

                        string filepath = sExePath + "\\Types.sdt";
                        FileStream fs = new FileStream(filepath, FileMode.Create);
                        StreamWriter sw = new StreamWriter(fs);
                        sw.WriteLine(input);
                        sw.Flush();
                        sw.Close();
                        fs.Close();
                        sColor.TextColor((int)ConsoleFontColor.Foreground.Green + (int)ConsoleFontColor.Foreground.Intensity);
                        Console.WriteLine("    Command Completed.");
                        sColor.ResetColor();
                        break;
                    case 'g':
                        StreamReader objReader = new StreamReader(sExePath + "\\Types.sdt");
                        string sLine = "";
                        sLine = objReader.ReadLine();
                        Console.WriteLine("  " + sLine);
                        objReader.Close();
                        break;
                    default:
                        Console.WriteLine("  No such command, sorry.");
                        break;
                }

                return;
            }

            string typeFilePath = sExePath + "\\Types.sdt";
            var codeLines = new CodeLines(GetWantedFileType(typeFilePath), sColor);

            String startPath = "";
            foreach (string s in args)
            {
                startPath += (s + ' ');
            }

            codeLines.doCounting(startPath);

            codeLines.OutputResult();
        }
    }
}
